package com.xinguan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinguan.entity.Types;
import com.xinguan.mapper.TypesMapper;
import com.xinguan.service.TypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 陈俊行
 * @Date: 2023/11/07/9:20
 * @Description:
 */
@Service
public class TypesServiceImpl extends ServiceImpl<TypesMapper, Types> implements TypesService {
    @Autowired
    private TypesMapper typesMapper;
    @Override
    public boolean increaseCategory(Types types) {
        LambdaQueryWrapper<Types> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Types::getCategoryName,types.getCategoryName());
        Types types2 = typesMapper.selectOne(wrapper);
        if(Objects.isNull(types2)){
            String uuid = UUID.randomUUID().toString().replace("-", "");
            Types types1 =new Types();
            types1.setCategoryId(uuid);
            types1.setCategoryName(types.getCategoryName());
            //types1.setCurToal(types.getCurToal());
            typesMapper.insert(types1);
            return true;
        }else{
//            types2.setCategoryName(types.getCategoryName());
//            types2.setCurToal(types.getCurToal());
//            typesMapper.insert(types2);
            return false;
        }
    }

    @Override
    public PageInfo<Types> getTypesPage(Integer current, Integer pageSize) {
        PageHelper.startPage(current,pageSize);
        LambdaQueryWrapper<Types> wrapper = new LambdaQueryWrapper<>();
        List<Types> typesList = typesMapper.selectList(wrapper);
        return new PageInfo<>(typesList);
    }

    @Override
    public Boolean deleteTypes(String id, Integer curToal) {
        LambdaQueryWrapper<Types> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Types::getCategoryId,id);
        Types types = typesMapper.selectOne(wrapper);
        if(types.getCurToal()==0){
            typesMapper.delete(wrapper);
            return false;
        }else {
            return true;
        }

    }
}
